using System;
using System.Collections.Generic;
using BikeSystem.Models;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Config;
namespace BikeSystem.DAL
{
    public class LoginHistoryService
    {

        readonly static string YapFileName = Path.Combine(Environment.CurrentDirectory, "bikesystem.yap");

        public long InsertHistory(LoginHistory history)
        {

            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {
                db.Store(history);
                db.Ext().Commit();

                long histId = db.Ext().GetID(history);

                Console.WriteLine("Stored {0}", histId);

                return histId;
            }
        
        }

        public void UpdateOverTime(long  LoginHistoryId)
        {
            IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(LoginHistory)).CascadeOnUpdate(true);
            using (IObjectContainer db = Db4oEmbedded.OpenFile(config, YapFileName))
            {
                LoginHistory hist = (LoginHistory)db.Ext().GetByID(LoginHistoryId);
                hist.OverTime = DateTime.Now;
                db.Store(hist);
            }
        }

        public IList<LoginHistory> GetHistories()
        {

            IList<LoginHistory> hists = new List<LoginHistory>();
            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {
                IObjectSet result = db.QueryByExample(typeof(LoginHistory));
                while (result.HasNext())
                {
                    hists.Add((LoginHistory)result.Next());
                }

                return hists;
            };
        }



    }
}
